package kizzo.gui 
{
	import flash.display.Sprite;		
	import org.flashdevelop.utils.FlashConnect;
	
	/**
	 * ...
	 * @author kizzo
	 */
	public class PropertyMenu extends Sprite
	{
		//private var edit_objects:;
		
		
		private var text_input_num:int;
		
		public var close_button:Sprite;
		
		private var edit_objects:Vector.<EditObject>;		
		private var text_inputs:Vector.<LabelledTextInput>;
		private var static_texts:Vector.<LabelledTextInput>;
		
		public function PropertyMenu(height:Number, width:Number) 
		{
			this.graphics.beginFill(0xffffff, .8);			
			this.graphics.drawRect(0, 0, height, width);
			//this.text_inputs = [];
			this.text_input_num = 0;
			
			this.close_button = new Sprite();
			this.close_button.graphics.beginFill(0, 1);
			this.close_button.graphics.drawRect(0, 0, 100, 20);
			
			this.close_button.x = 200;
			this.close_button.y = 160;
			
			this.edit_objects = new Vector.<EditObject>();	
			this.text_inputs = new Vector.<LabelledTextInput>();
			this.static_texts = new Vector.<LabelledTextInput>();
			
			addChild(close_button);
		}	
		
		
		private function add_text_input(ti:LabelledTextInput):void
		{			
			this.addChild(ti);			
			ti.x = 10;
			ti.y = 10 + text_input_num * 20;			
			text_input_num++;
		}
		
		public function add_static_text(ti:LabelledTextInput):void
		{
			this.static_texts.push(ti);
			add_text_input(ti);
		}
				
		
		
		public function add_edit_object(edit_object:EditObject):void
		{	
			var text_input:LabelledTextInput = new LabelledTextInput(edit_object.prop, String(edit_object.edit_object[edit_object.prop]));
			
			this.edit_objects.push(edit_object);
			this.text_inputs.push(text_input);
			
			add_text_input(text_input);
		}
		
		
		public function apply_edits():void
		{
			for (var i:int = 0; i < this.edit_objects.length; i++)
			{				
				edit_objects[i].edit_object[edit_objects[i].prop] = edit_objects[i].obj_class(text_inputs[i].value.text);
				removeChild(text_inputs[i]);
			}
			for ( i = 0; i < this.static_texts.length; i++)	
			{
				removeChild(static_texts[i]);
			}
		}
		
		
		public function clear():void
		{			
			this.edit_objects = new Vector.<EditObject>();
			this.text_inputs = new Vector.<LabelledTextInput>();
			
			this.static_texts = new Vector.<LabelledTextInput>();			
			
			text_input_num = 0;
		}
		
		
	}

}